class CategoryGroup < ActiveRecord::Base
  has_many :categories
  has_many :roots, :class_name => 'Category', :conditions => "parent_id IS NULL"

  after_save :change_root_url_permalinks

  has_paper_trail :ignore => [:updated_at, :created_at, :id]
  diff :exclude => [:updated_at, :created_at]
  
  validates :presentation, :root_url, :length => {:maximum => 30}, :presence => true
  validates :root_url, :format => {:with => /^\/$|^\/[a-zA-Z0-9]+\/$/}

  def deletable?
    categories
  end

  def change_root_url_permalinks
    unless self.categories.blank?
      if self.root_url_changed?
        self.roots.each do |category|
          category.make_permalink
          category.save
        end
      end
    end
  end
end